package org.jeecg.modules.appuser.job;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.Date.DateUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.appuser.constant.TaskNamesConstant;
import org.jeecg.modules.appuser.entity.TbUserGameBuiding;
import org.jeecg.modules.appuser.entity.TbUserIntegralConfig;
import org.jeecg.modules.appuser.entity.TbUserIntegralConfigDetail;
import org.jeecg.modules.appuser.service.ITbUserGameBuidingService;
import org.jeecg.modules.appuser.service.ITbUserIntegralConfigDetailService;
import org.jeecg.modules.appuser.service.ITbUserIntegralConfigService;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 任务更新
 */
public class IntegralConfigEveryDayTask implements Job {

    @Autowired
    private ITbMnUserService tbMnUserService;

    @Autowired
    private ITbUserIntegralConfigService tbUserIntegralConfigService;

    @Autowired
    private ITbUserIntegralConfigDetailService tbUserIntegralConfigDetailService;

    @Autowired
    private ITbUserGameBuidingService tbUserGameBuidingService;

    //每天晚上12点清楚 新建任务
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        List<TbMnUser> tbMnUserList = tbMnUserService.list();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state", 1);
        List<TbUserIntegralConfig> tbUserIntegralConfigList = tbUserIntegralConfigService.list(queryWrapper);
        if (null != tbMnUserList && tbMnUserList.size() > 0) {
            tbMnUserList.forEach(tbMnUser -> {

                if (null != tbUserIntegralConfigList && tbUserIntegralConfigList.size() > 0) {

                    for (TbUserIntegralConfig userIntegralConfig : tbUserIntegralConfigList) {

                        TbUserIntegralConfigDetail tbUserIntegralConfigDetail = new TbUserIntegralConfigDetail();
                        BeanUtil.copyProperties(userIntegralConfig, tbUserIntegralConfigDetail);
                        tbUserIntegralConfigDetail.setId(UUIDGenerator.generate());
                        Calendar date = Calendar.getInstance();
                        date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 0, 0, 0);
                        tbUserIntegralConfigDetail.setCreateTime(date.getTime());
                        Calendar date1 = Calendar.getInstance();
                        date1.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 23, 59, 59);
                        tbUserIntegralConfigDetail.setEndTime(date1.getTime());
                        tbUserIntegralConfigDetail.setUserId(tbMnUser.getId());

                        QueryWrapper queryWrapper2 = new QueryWrapper();
                        queryWrapper2.eq("task_name", tbUserIntegralConfigDetail.getTaskName());
                        queryWrapper2.eq("user_id", tbMnUser.getInviteCode());
                        queryWrapper2.ge("create_time", DateUtil.calcDate(0));
                        queryWrapper2.le("end_time", DateUtil.calcDate(1));
                        queryWrapper2.eq("state", 1);
                        TbUserIntegralConfigDetail tbUserIntegralConfigDetail1 = tbUserIntegralConfigDetailService.getOne(queryWrapper2);
                        if (null == tbUserIntegralConfigDetail1) {
                            QueryWrapper queryWrapper3 = new QueryWrapper();
                            queryWrapper3.eq("task_name", tbUserIntegralConfigDetail.getTaskName());
                            queryWrapper3.eq("user_id", tbMnUser.getInviteCode());
                            queryWrapper3.eq("state", 1);
                            List<TbUserIntegralConfigDetail> tbUserIntegralConfigDetail1List =   tbUserIntegralConfigDetailService.list(queryWrapper3);
                            if(null!=tbUserIntegralConfigDetail1List&&tbUserIntegralConfigDetail1List.size()>0) {
                                for (TbUserIntegralConfigDetail userIntegralConfigDetail : tbUserIntegralConfigDetail1List) {
                                    userIntegralConfigDetail.setState(0);
                                }
                                tbUserIntegralConfigDetailService.updateBatchById(tbUserIntegralConfigDetail1List);
                            }
                            //如果是签到任务 设置规则
                            if(tbUserIntegralConfigDetail.getTaskName().equals(TaskNamesConstant.Task_QIAN_DAO)){
                             //   tbUserIntegralConfigDetail.setWeekNumber(DateUtil.getCurWeekNoOfYear());getCurWeekNoOfMonth
                                tbUserIntegralConfigDetail.setWeekNumber(DateUtil.getCurWeekNoOfYear());
                                //设置今天星期几
                                tbUserIntegralConfigDetail.setWhichDayWeek(DateUtil.getCurWeekNo());
                                tbUserIntegralConfigDetail.setSigninStatus(0);
                            }
                            tbUserIntegralConfigDetail.setCompleteState(0);// 设置完成状态

                            tbUserIntegralConfigDetailService.save(tbUserIntegralConfigDetail);
                        }
                    }
                }
            });
        }
    }

    public void clearGameBuildingHours(){

        List<TbUserGameBuiding> tbUserGameBuidingList = tbUserGameBuidingService.list();
        if(null!=tbUserGameBuidingList&&tbUserGameBuidingList.size()>0){
            tbUserGameBuidingList.forEach(tbUserGameBuiding -> {
                //清空当前的总营业时间
                tbUserGameBuiding.setTotalConsumeHours(0);
                tbUserGameBuidingService.updateById(tbUserGameBuiding);
            });

        }



    }
}
